package org.springframework.ai.openai.samples.helloworld.simple;

import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.ai.openai.OpenAiChatOptions;
import org.springframework.ai.openai.api.OpenAiImageApi;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.function.ServerResponse;

import java.util.List;

@RestController
@RequiredArgsConstructor
public class FunctionCallController {

    private final OpenAiChatModel chatClient;

    /**
     *
     *   function-call 应对大模型无法获取实时信息的弊端
     *   结合Function bean一起 使用
     *   @see org.springframework.ai.openai.samples.helloworld.callbacks.AIConfigs#chatClient(org.springframework.ai.chat.client.ChatClient.Builder)
     *
     *  https://docs.spring.io/spring-ai/reference/1.0/api/chat/functions/openai-chat-functions.html
     * @param message
     * @return
     */
    @GetMapping("/ai/fc")
    public String fc(@RequestParam(value = "message", defaultValue = "长沙有多少个叫徐庶的人?") String message){
        UserMessage userMessage =new UserMessage(message);

        ChatResponse response = chatClient.call(new Prompt(
                List.of(userMessage),
                OpenAiChatOptions.builder()
                        .withFunction("getLocationAndNum")
                        .withModel("gpt-4-turbo")
                        .build()));

        return response.getResult().getOutput().getContent();
    }
}
